package com.web.base.open.service;

import com.blue.jdbc.JdbcDataSource;
import com.web.base.open.dao.RoleAccountDao;
import com.web.base.open.model.Role;
import com.web.base.open.model.RoleAccount;
import com.web.core.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 开放平台角色-帐号业务层
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2017年02月27日
 */
@Service("openRoleAccountService")
@Transactional
@JdbcDataSource(Const.DS_BASE)
public class RoleAccountService
{
	@Autowired
	private RoleAccountDao roleAccountDao;
	
	public RoleAccountService()
	{
	}
	
	public List<RoleAccount> list(RoleAccount roleAccount)
	{
		List<RoleAccount> list = roleAccountDao.list(roleAccount);
		return list;
	}
	
	public List<Integer> listByAccount(Integer accountId)
	{
		return roleAccountDao.listByAccount(accountId);
	}
	
	public List<Integer> listByAccount(String accountName)
	{
		return roleAccountDao.listByAccount(accountName);
	}
	
	public List<Role> listSelect(Integer accountId)
	{
		return roleAccountDao.listSelect(accountId);
	}
	
	public void save(RoleAccount roleAccount)
	{
		roleAccountDao.deleteByAccount(roleAccount.getAccountId());
		
		if (roleAccount.getRoleIds() != null && !roleAccount.getRoleIds().isEmpty())
		{
			List<RoleAccount> roleAccountList = new ArrayList<>();
			for (String roleId : roleAccount.getRoleIds().split(","))
			{
				roleAccountList.add(new RoleAccount(Integer.valueOf(roleId), roleAccount.getAccountId()));
			}
			roleAccountDao.saveList(roleAccountList);
		}
		
	}
	
}
